﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class Business_approvehistory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind(true);
        }
    }
    //分页
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        bind(true);
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        bind(false);
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //实例化计划类
        int row = Convert.ToInt32(e.CommandArgument);
        PM.PMBusiness.approveschedual app = new PM.PMBusiness.approveschedual();
        int id = Convert.ToInt32(GridView1.DataKeys[row].Value);
        string approvecode = this.GridView1.Rows[row].Cells[1].Text.Trim();
        string plancode = this.GridView1.Rows[row].Cells[2].Text.Trim();
        if (e.CommandName == "deleteplan")
        {
            if (app.Delete_ApprovePlan(approvecode,plancode) > 0)
            {
                PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "已删除此项目计划！");
                bind(false);
            }
        }
    }
    //行绑定
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[4].Text.Trim() != string.Empty && e.Row.Cells[5].Text.Trim() != string.Empty)
            {
                DateTime nowtime = DateTime.Now;
                //因为RowDataBound是发生在数据绑定之后，所以我们可以
                //判断时间来确定其状态
                if (DateTime.Parse(e.Row.Cells[4].Text.Trim()) > nowtime)
                {
                    e.Row.Cells[3].Text = "未执行";
                }
                else if (DateTime.Parse(e.Row.Cells[5].Text.Trim()) < nowtime)
                {
                    e.Row.Cells[3].Text = "已过期";
                    Image img = (Image)(e.Row.Cells[6].Controls[0]);
                    img.ImageUrl = "~/js/themes/icons/disable_pencil.png";
                    e.Row.Cells[6].Enabled = false;
                }
                else
                {
                    e.Row.Cells[3].Text = "正在审批";
                    ImageButton ib = (ImageButton)e.Row.FindControl("imgbtndel");
                    ib.Enabled = false;
                    ib.ImageUrl = "~/js/themes/icons/disable_cancel.png";
                    ib.ToolTip = "正在审批中，无法删除！";
                    Image img = (Image)(e.Row.Cells[6].Controls[0]);
                    img.ImageUrl = "~/js/themes/icons/disable_pencil.png";
                    e.Row.Cells[6].Enabled = false;
                }
            }
            else
                e.Row.Cells[3].Text = "修改中";
        }
    }
    //行编辑
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex;
        bind(false);
        TextBox txt1 = (TextBox)(this.GridView1.Rows[e.NewEditIndex].Cells[4].Controls[0]);
        TextBox txt2 = (TextBox)(this.GridView1.Rows[e.NewEditIndex].Cells[5].Controls[0]);
        txt1.Attributes.Add("id", "txtOpenDate");
        txt2.Attributes.Add("id", "txtCloseDate");
        txt1.Attributes.Add("onfocus", "WdatePicker({autoPickDate:true,dateFmt:'yyyy/MM/dd HH:mm:ss',alwaysUseStartDate:true,isShowClear:false,readOnly:true,maxDate:'#F{$dp.$D(\\'txtCloseDate\\',{d:-1})||\\'2020-10-01\\'}'})");
        txt2.Attributes.Add("onfocus", "WdatePicker({autoPickDate:true,startDate:'%y-%M-01 00:00:00',dateFmt:'yyyy/MM/dd HH:mm:ss',alwaysUseStartDate:true,isShowClear:false,readOnly:true,minDate:'#F{$dp.$D(\\'txtOpenDate\\',{d:+1})}',maxDate:'2020-10-01'})");
    }
    //行更新
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        PM.PMBusiness.approveschedual app = new PM.PMBusiness.approveschedual();
        DateTime adate = DateTime.Now;
        //获取行主键值和类型值
        int id = Convert.ToInt16(GridView1.DataKeys[e.RowIndex].Value);
        string type = GridView1.Rows[e.RowIndex].Cells[2].Text.Trim();
        //获取修改值
        string opendate = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.Trim();
        string closedate = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text.Trim();
        if (opendate == string.Empty || closedate == string.Empty)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "时间不能为空！");
            return;
        }
        if (DateTime.Parse(opendate.Trim()) < adate)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "开启时间不能小于现在时间！");
            return;
        }
        if (app.Update_ApprovePlan(id, opendate, closedate) != 0)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "更新成功！");
            this.GridView1.EditIndex = -1;
        }
        bind(false);
    }
    //搜索
    protected void btnSearch_Click(object sender, ImageClickEventArgs e)
    {
        this.AspNetPager1.CurrentPageIndex = 0;
        bind(true);
    }
    /// <summary>
    /// 绑定gridview
    /// </summary>
    /// <param name="getCount">是否获取总数</param>
    protected void bind(bool getCount)
    {
        try
        {
            StringBuilder sqlstr = new StringBuilder(" 1=1 ");
            getWhere(ref sqlstr);
            PM.Code.Web.controlbind.BindGridViewPublicProc(GridView1, AspNetPager1, "Bus_ApprovePlan", "id", "*", "id", sqlstr.ToString(), true);
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
    }

    /// <summary>
    /// 生成sql语句
    /// </summary>
    /// <param name="sqlstrwhere">sql语句</param>
    protected void getWhere(ref StringBuilder sqlstr)
    {
        if (this.txtApproveCode.Text.Trim() != string.Empty)
        {
            sqlstr.Append(" and approvecode='").Append(this.txtApproveCode.Text.Trim()).Append("'");
            return;
        }
        if (this.txtPlanCode.Text.Trim() != string.Empty)
        {
            sqlstr.Append(" and plancode='").Append(this.txtPlanCode.Text.Trim()).Append("'");
            return;
        }
        else
        {
            if (this.txtTime1.Value.Trim() != string.Empty && this.txtTime2.Value.Trim() != string.Empty)
                sqlstr.Append(" and builtdate>='").Append(this.txtTime1.Value.Trim()).Append("' and builtdate<='").Append(this.txtTime2.Value.Trim()).Append("' ");
            if (this.ddlStatus.SelectedIndex != 0)
            {
                DateTime nowtime = DateTime.Now;
                if (this.ddlStatus.SelectedValue.Equals("2"))
                    sqlstr.Append(" and closetime<'").Append(nowtime.ToString()).Append("'");
                else if (this.ddlStatus.SelectedValue.Equals("3"))
                    sqlstr.Append(" and opentime<='").Append(nowtime.ToString()).Append("' and closetime>='").Append(nowtime.ToString()).Append("'");
                else if (this.ddlStatus.SelectedValue.Equals("4"))
                    sqlstr.Append(" and opentime>'").Append(nowtime.ToString()).Append("'");
            }
        }
    }
}
